package com.ms.education.course.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.education.course.entity.Video;
import com.ms.education.course.mapper.VideoMapper;
import com.ms.education.course.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.education.course.service.feign.VodFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-01-18
 */
@Service
public class VideoServiceImp extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private VodFeignService vodFeignService;

    /**
     * @param idList 课程id
     */
    @Override
    public void removeBatchByCourseIds(List<Integer> idList) {
        //查询课程所关联的视频id
        QueryWrapper<Video> wrapper = new QueryWrapper<Video>().in("course_id", idList);
        List<Video> videos = list(wrapper);
        List<String> videoIds = videos.stream().map(Video::getVideoSourceId).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
        String join = StrUtil.join(",", videoIds);
        if (!StringUtils.isEmpty(join)) {
            vodFeignService.removeBatchVideoByIds(join);
        }
        List<Integer> ids = videos.stream().map(Video::getId).collect(Collectors.toList());
        removeBatchByIds(ids);
    }

    /**
     * @param chapterId 章节id
     */
    @Override
    public void removeVideoByChapterId(Integer chapterId) {
        //查询章节关联的视频
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        List<Video> videos = this.list(wrapper);
        List<String> videoIds = videos.stream().map(Video::getVideoSourceId).filter(s -> !StringUtils.isEmpty(s)).collect(Collectors.toList());
        String join = StrUtil.join(",", videoIds);
        if (!StringUtils.isEmpty(join)) {
            vodFeignService.removeBatchVideoByIds(join);
        }
        //删除小节
        List<Integer> vs = videos.stream().map(Video::getId).collect(Collectors.toList());
        this.removeBatchByIds(vs);
    }

    @Override
    public void deleteVideoById(Video video) {
        //删除视频
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            vodFeignService.removeBatchVideoByIds(videoSourceId);
        }
        //删除小节
        this.removeById(video.getId());
    }

    //获取视频频播放数
    @Override
    public Map<String, Integer> getVideoPlayNums(String[] videoSourceIds) {
        HashMap<String, Integer> res = new HashMap<>();
        for (String videoSourceId : videoSourceIds) {
            Long size = redisTemplate.opsForHyperLogLog().size("specific-video-play:" + videoSourceId);
            res.put(videoSourceId, Math.toIntExact(size));
        }
        return res;
    }
}
